package com.gezzell.service;

import com.gezzell.bean.Command;
import com.gezzell.bean.CommandContent;
import com.gezzell.bean.Message;
import com.gezzell.dao.CommandDao;
import com.gezzell.dao.MessageDao;
import com.gezzell.util.Iconst;

import java.util.List;
import java.util.Random;

/**
 * 列表相关的业务功能
 */
public class QueryService {
    public List<Message> queryMessageList(String command, String description){
        MessageDao messageDao = new MessageDao();
        return messageDao.queryMessageList(command, description);
    }


    /**
     * 通过指令查询自动回复的内容
     */

//    public String queryByCommand(String command) {
//        MessageDao messageDao = new MessageDao();
//        List<Message> messageList;
//        if(Iconst.HELP_COMMAND.equals(command)){
//            messageList = messageDao.queryMessageList(null, null);
//            StringBuilder result = new StringBuilder();
//            for(int i = 0; i < messageList.size(); i++){
//                if(i != 0){
//                    result.append("<br/>");
//                }
//                result.append("回复[" + messageList.get(i).getCommand() + "]可以查看" + messageList.get(i).getDescription());
//            }
//            return result.toString();
//        }
//        messageList = messageDao.queryMessageList(command, null);
//        if(messageList.size() > 0){
//            return messageList.get(0).getContent();
//        }
//        return Iconst.NO_MATCHING_CONTENT;
//    }

    /**
     * 通过指令查询自动回复的内容
     */

    public String queryByCommand(String command) {
        CommandDao commandDao = new CommandDao();
        List<Command> commandList;
        if(Iconst.HELP_COMMAND.equals(command)){
            commandList = commandDao.queryCommandList(null, null);
            StringBuilder result = new StringBuilder();
            for(int i = 0; i < commandList.size(); i++){
                if(i != 0){
                    result.append("<br/>");
                }
                result.append("回复[" + commandList.get(i).getName() + "]可以查看" + commandList.get(i).getDescription());
            }
            return result.toString();
        }
        commandList = commandDao.queryCommandList(command, null);
        if(commandList.size() > 0){
            List<CommandContent> contentList = commandList.get(0).getContentList();
            int i = new Random().nextInt(contentList.size());
            return contentList.get(i).getContent();
        }
        return Iconst.NO_MATCHING_CONTENT;
    }
}
